﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using SmartSpace.Core;
using System;
using SsTool.Common;
using System.Windows.Input;

namespace SsTool.Core.Parts
{
    /// <summary>
    /// Interaction logic for NavigationPart.xaml
    /// </summary>
    public partial class NavigationPart : UserControl
    {
        /// <summary>
        /// 新建时使用的navigation
        /// </summary>
        private Navigation _newNavigation;

        private SmartSpace.Core.Application _application;

        public NavigationPart()
        {
            InitializeComponent();
        }

        public void SetApplication(SmartSpace.Core.Application application)
        {
            _application = application;
            LoadDatas();
        }

        private void LoadDatas()
        {
            if (_application == null)
            {
                return;
            }

            ICoreService service = App.Get<ICoreService>();
            Navigation parent = new Navigation();
            parent.Caption = "导航列表";
            List<Navigation> items = new List<Navigation>();
            items.Add(parent);
            parent.Children = service.GetNavigations(_application.Id);
            _ctlTree.ItemsSource = items;
        }

        private void _ctlBtnCreate_Click(object sender, RoutedEventArgs e)
        {
            if (_newNavigation != null)
            {
                if (MessageBox.Show("是否要放弃正在新建的导航信息?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK)
                {
                    return;
                }
            }

            Navigation selectedNavigation = _ctlTree.SelectedItem as Navigation;

            _newNavigation = new Navigation();
            _newNavigation.Id = Guid.NewGuid();
            _newNavigation.ApplicationId = _application.Id;
            if (selectedNavigation != null && selectedNavigation.ParentId != Guid.Empty)
            {
                _newNavigation.ParentId = selectedNavigation.Id;
            }

            this._ctlNavigationPanel.DataContext = _newNavigation;
            this._ctlNavigationPanel.IsEnabled = true;
        }

        private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            SelectViewWindow window = new SelectViewWindow();
            if (window.ShowDialog() == true)
            {
                _newNavigation.ViewId = window.SelectedView.Id;

                ICoreService service = App.Get<ICoreService>();
                if (_newNavigation.ViewId != null)
                {
                    ModuleView view = service.GetView(_newNavigation.ViewId.Value);
                    _ctlEditView.Text = view.Caption;
                }

                this._ctlNavigationPanel.DataContext = null;
                this._ctlNavigationPanel.DataContext = _newNavigation;
                this._ctlNavigationPanel.IsEnabled = true;
            }
        }

        private void _ctlSubmit_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("是否要保存导航信息?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK)
            {
                return;
            }

            ICoreService service = App.Get<ICoreService>();
            if (service.StoreNavigation(_newNavigation) == true)
            {
                _newNavigation = null;

                //Navigation selectedNavigation = _ctlTree.SelectedItem as Navigation;
                //this._ctlNavigationPanel.DataContext = selectedNavigation;

                //this._ctlNavigationPanel.IsEnabled = false;
                LoadDatas();
            }
        }

        private void _ctlBtnSelectActivity_Click(object sender, RoutedEventArgs e)
        {
            SelectActivityWindow window = new SelectActivityWindow();
            if (window.ShowDialog() == true)
            {
                _newNavigation.Arguments = string.Format("workflowid={0}&activityid={1}",
                   window.SelectedActivity.Workflow.Id,
                   window.SelectedActivity.Id);
                this._ctlNavigationPanel.DataContext = null;
                this._ctlNavigationPanel.DataContext = _newNavigation;
                this._ctlNavigationPanel.IsEnabled = true;
            }
        }

        private void _ctlTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (_newNavigation != null)
            {
                if (MessageBox.Show("是否要放弃正在编辑的导航信息?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK)
                {
                    return;
                }
            }

            _newNavigation = null;
            Navigation selectedNavigation = e.NewValue as Navigation;

            if (selectedNavigation != null)
            {
                ICoreService service = App.Get<ICoreService>();
                if (selectedNavigation.ViewId != null)
                {
                    ModuleView view = service.GetView(selectedNavigation.ViewId.Value);
                    _ctlEditView.Text = view.Caption;
                }
                else
                {
                    _ctlEditView.Text = string.Empty;
                }

                this._ctlNavigationPanel.DataContext = selectedNavigation;
                this._ctlNavigationPanel.IsEnabled = false;
            }
            else
            {
                this._ctlNavigationPanel.DataContext = null;
                this._ctlNavigationPanel.IsEnabled = false;
            }
        }

        private void _ctlBtnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (_newNavigation != null)
            {
                if (MessageBox.Show("是否要放弃正在编辑的导航信息?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question) != MessageBoxResult.OK)
                {
                    return;
                }
            }

            Navigation selectedNavigation = _ctlTree.SelectedItem as Navigation;

            _newNavigation = selectedNavigation;


            ICoreService service = App.Get<ICoreService>();
            if (_newNavigation.ViewId != null)
            {
                ModuleView view = service.GetView(_newNavigation.ViewId.Value);
                _ctlEditView.Text = view.Caption;
            }
            else
            {
                _ctlEditView.Text = string.Empty;
            }

            this._ctlNavigationPanel.DataContext = _newNavigation;
            this._ctlNavigationPanel.IsEnabled = true;
        }

        private void _ctlSetDefault_Click(object sender, RoutedEventArgs e)
        {
            Navigation selectedNavigation = _ctlTree.SelectedItem as Navigation;
            if (selectedNavigation != null)
            {
                selectedNavigation.IsDefault = true;
                
                ICoreService service = App.Get<ICoreService>();
                if (service.StoreNavigation(selectedNavigation) == true)
                {
                    MessageBox.Show("设置成功!");
                }
                else
                {
                    MessageBox.Show("设置失败!");
                }
            }
        }

        private void _ctlSetShortcut_Click(object sender, RoutedEventArgs e)
        {
            Navigation selectedNavigation = _ctlTree.SelectedItem as Navigation;
            if (selectedNavigation != null)
            {
                ICoreService service = App.Get<ICoreService>();
                if (service.AddShortcut(selectedNavigation.Id, string.Empty) == true)
                {
                    MessageBox.Show("设置成功!");
                }
                else
                {
                    MessageBox.Show("设置失败!");
                }
            }
        }
    }
}
